package com.guigu.www.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.www.pojo.Roles;
import com.guigu.www.pojo.PageVo;
import com.guigu.www.service.RolesService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
public class RolesController {
    @Autowired
    RolesService service;
    @RequestMapping("queryroles.action")
    public PageVo<Roles> queryroles(Roles roles,
                                    @RequestParam(value = "pagenum",defaultValue = "1")int pagenum,
                                    @RequestParam(value = "pagesize",defaultValue = "5")int pagesize){
        QueryWrapper queryWrapper=new QueryWrapper();
        if(StringUtils.isNotBlank(roles.getName())){
            queryWrapper.like("name",roles.getName());
        }
        Page<Roles> page=service.page(new Page<Roles>(pagenum,pagesize),queryWrapper);
        PageVo<Roles> pageVo=new PageVo<>();
        pageVo.setRows(page.getRecords());
        pageVo.setTotal(page.getTotal());
        return pageVo;
    }
    @RequestMapping("addroles.action")
    public Map addroles(Roles roles){
        Map map=new HashMap();
        boolean res=service.save(roles);
        if(res){
            map.put("msg","添加成功");
            map.put("code","1");
        }else {
            map.put("msg","添加失败");
            map.put("code","0");
        }
        return map;
    }

    @RequestMapping("deleteroles.action/{id}")
    public Map deleteroles(@PathVariable int id){
        Map map=new HashMap();
        boolean res=service.removeById(id);
        if(res){
            map.put("msg","删除成功");
            map.put("code","1");
        }else {
            map.put("msg","删除失败");
            map.put("code","0");
        }
        return map;
    }
    @RequestMapping("updateroles.action")
    public Map updateroles(Roles roles){
        Map map=new HashMap();
        boolean res=service.updateById(roles);
        if(res){
            map.put("msg","修改成功");
            map.put("code","1");
        }else {
            map.put("msg","修改失败");
            map.put("code","0");
        }
        return map;
    }
    @RequestMapping("querybyrolesid.action/{id}")
    public Roles querybyrolesid(@PathVariable  int id){
        Roles res=service.getById(id);
        return res;
    }
    @GetMapping("querybyroleid11.action/{id}")
    public Roles querybyroleid(@PathVariable int id){
        Roles res=service.getById(id);
        return res;
    }
}
